package code.example.base.gc.gc5;

import code.example.base.gc.Person;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

/**
 *
 *  java -Xms512m -Xmx512m -Xmn130m -XX:SurvivorRatio=3 -XX:+PrintGCTaskTimeStamps -XX:+PrintTenuringDistribution -XX:-UseAdaptiveSizePolicy -XX:+PrintGCDetails -XX:+PrintHeapAtGC -cp ./lib/*:*.jar -XX:+UseG1GC  code.example.base.gc.gc5.TriggerGC5
 *   程序不变，垃圾回收器改为G1，回收时间由 120ms-->30ms.
 *
 *   {Heap before GC invocations=155 (full 0):
 garbage-first heap   total 524288K, used 134461K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 130 young (133120K), 16 survivors (16384K)
 Metaspace       used 2952K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
 [GC pause (G1 Evacuation Pause) (young)
 Desired survivor size 23068672 bytes, new threshold 15 (max 15)
 - age   1:   14428632 bytes,   14428632 total
 , 0.0302980 secs]
 [Parallel Time: 28.4 ms, GC Workers: 8]
 [GC Worker Start (ms): Min: 1625075.2, Avg: 1625075.3, Max: 1625075.5, Diff: 0.3]
 [Ext Root Scanning (ms): Min: 0.3, Avg: 0.4, Max: 0.6, Diff: 0.3, Sum: 3.6]
 [Update RS (ms): Min: 2.0, Avg: 2.9, Max: 4.4, Diff: 2.4, Sum: 22.8]
 [Processed Buffers: Min: 1, Avg: 1.4, Max: 2, Diff: 1, Sum: 11]
 [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.3]
 [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
 [Object Copy (ms): Min: 22.8, Avg: 24.3, Max: 25.0, Diff: 2.2, Sum: 194.4]
 [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.0, Sum: 0.3]
 [Termination Attempts: Min: 1, Avg: 23.8, Max: 39, Diff: 38, Sum: 190]
 [GC Worker Other (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 0.9]
 [GC Worker Total (ms): Min: 27.5, Avg: 27.8, Max: 27.9, Diff: 0.4, Sum: 222.2]
 [GC Worker End (ms): Min: 1625103.0, Avg: 1625103.1, Max: 1625103.2, Diff: 0.2]
 [Code Root Fixup: 0.0 ms]
 [Code Root Purge: 0.0 ms]
 [Clear CT: 0.5 ms]
 [Other: 1.4 ms]
 [Choose CSet: 0.0 ms]
 [Ref Proc: 0.5 ms]
 [Ref Enq: 0.0 ms]
 [Redirty Cards: 0.6 ms]
 [Humongous Register: 0.0 ms]
 [Humongous Reclaim: 0.0 ms]
 [Free CSet: 0.1 ms]
 [Eden: 114.0M(114.0M)->0.0B(114.0M) Survivors: 16.0M->16.0M Heap: 131.3M(512.0M)->17.3M(512.0M)]
 Heap after GC invocations=156 (full 0):
 garbage-first heap   total 524288K, used 17725K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 16 young (16384K), 16 survivors (16384K)
 Metaspace       used 2952K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
 }
 [Times: user=0.23 sys=0.00, real=0.03 secs]
 {Heap before GC invocations=156 (full 0):
 garbage-first heap   total 524288K, used 134461K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 130 young (133120K), 16 survivors (16384K)
 Metaspace       used 2952K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
 [GC pause (G1 Evacuation Pause) (young)
 Desired survivor size 23068672 bytes, new threshold 15 (max 15)
 - age   1:   14401632 bytes,   14401632 total
 , 0.0313610 secs]
 [Parallel Time: 29.3 ms, GC Workers: 8]
 [GC Worker Start (ms): Min: 1635493.2, Avg: 1635493.4, Max: 1635493.6, Diff: 0.4]
 [Ext Root Scanning (ms): Min: 0.2, Avg: 0.4, Max: 0.6, Diff: 0.4, Sum: 3.0]
 [Update RS (ms): Min: 1.7, Avg: 2.7, Max: 3.9, Diff: 2.3, Sum: 21.9]
 [Processed Buffers: Min: 1, Avg: 1.4, Max: 2, Diff: 1, Sum: 11]
 [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.2, Diff: 0.2, Sum: 0.3]
 [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.1]
 [Object Copy (ms): Min: 24.3, Avg: 25.5, Max: 26.3, Diff: 2.0, Sum: 204.0]
 [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
 [Termination Attempts: Min: 1, Avg: 5.8, Max: 11, Diff: 10, Sum: 46]
 [GC Worker Other (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 0.8]
 [GC Worker Total (ms): Min: 28.5, Avg: 28.8, Max: 29.0, Diff: 0.5, Sum: 230.2]
 [GC Worker End (ms): Min: 1635522.1, Avg: 1635522.2, Max: 1635522.3, Diff: 0.2]
 [Code Root Fixup: 0.0 ms]
 [Code Root Purge: 0.0 ms]
 [Clear CT: 0.6 ms]
 [Other: 1.4 ms]
 [Choose CSet: 0.0 ms]
 [Ref Proc: 0.5 ms]
 [Ref Enq: 0.0 ms]
 [Redirty Cards: 0.6 ms]
 [Humongous Register: 0.0 ms]
 [Humongous Reclaim: 0.0 ms]
 [Free CSet: 0.1 ms]
 [Eden: 114.0M(114.0M)->0.0B(114.0M) Survivors: 16.0M->16.0M Heap: 131.3M(512.0M)->16.8M(512.0M)]
 Heap after GC invocations=157 (full 0):
 garbage-first heap   total 524288K, used 17213K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 16 young (16384K), 16 survivors (16384K)
 Metaspace       used 2952K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
 }
 [Times: user=0.24 sys=0.00, real=0.04 secs]
 {Heap before GC invocations=157 (full 0):
 garbage-first heap   total 524288K, used 133949K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 130 young (133120K), 16 survivors (16384K)
 Metaspace       used 2952K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
 [GC pause (G1 Evacuation Pause) (young)
 Desired survivor size 23068672 bytes, new threshold 15 (max 15)
 - age   1:   14410632 bytes,   14410632 total
 , 0.0272634 secs]
 [Parallel Time: 25.3 ms, GC Workers: 8]
 [GC Worker Start (ms): Min: 1645907.6, Avg: 1645907.8, Max: 1645908.0, Diff: 0.4]
 [Ext Root Scanning (ms): Min: 0.1, Avg: 0.3, Max: 0.4, Diff: 0.3, Sum: 2.1]
 [Update RS (ms): Min: 2.2, Avg: 3.0, Max: 4.0, Diff: 1.8, Sum: 23.6]
 [Processed Buffers: Min: 1, Avg: 1.5, Max: 2, Diff: 1, Sum: 12]
 [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.2, Diff: 0.2, Sum: 0.4]
 [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
 [Object Copy (ms): Min: 20.4, Avg: 21.3, Max: 21.9, Diff: 1.5, Sum: 170.7]
 [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
 [Termination Attempts: Min: 1, Avg: 1.9, Max: 3, Diff: 2, Sum: 15]
 [GC Worker Other (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 0.9]
 [GC Worker Total (ms): Min: 24.5, Avg: 24.7, Max: 24.9, Diff: 0.4, Sum: 197.8]
 [GC Worker End (ms): Min: 1645932.4, Avg: 1645932.5, Max: 1645932.6, Diff: 0.2]
 [Code Root Fixup: 0.0 ms]
 [Code Root Purge: 0.0 ms]
 [Clear CT: 0.7 ms]
 [Other: 1.3 ms]
 [Choose CSet: 0.0 ms]
 [Ref Proc: 0.4 ms]
 [Ref Enq: 0.0 ms]
 [Redirty Cards: 0.5 ms]
 [Humongous Register: 0.0 ms]
 [Humongous Reclaim: 0.0 ms]
 [Free CSet: 0.2 ms]
 [Eden: 114.0M(114.0M)->0.0B(114.0M) Survivors: 16.0M->16.0M Heap: 130.8M(512.0M)->17.3M(512.0M)]
 Heap after GC invocations=158 (full 0):
 garbage-first heap   total 524288K, used 17725K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 16 young (16384K), 16 survivors (16384K)
 Metaspace       used 2952K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
 }
 [Times: user=0.20 sys=0.00, real=0.03 secs]
 ^CHeap
 garbage-first heap   total 524288K, used 75069K [0x00000000e0000000, 0x00000000e0101000, 0x0000000100000000)
 region size 1024K, 73 young (74752K), 16 survivors (16384K)
 Metaspace       used 2961K, capacity 4490K, committed 4864K, reserved 1056768K
 class space    used 326K, capacity 386K, committed 512K, reserved 1048576K
 *
 *
 *
 *
 */
public class TriggerGC5 {

    public void test(){
        Queue data = new ArrayDeque(200000);
        for(int i=0;i<200000;i++){//  增大10000
            Person person = new Person();
            person.setName("TestK"+i);
            data.offer(person);
        }

        for(;;){

            for(int i=0;i<500;i++){
                Person person = new Person();
                person.setName("TestK"+i);
                data.offer(person);
            }
            for(int i=0;i<500;i++){
                data.poll();
            }
            //LogFactory.getLog().info("queue.size="+data.size());
            try {
                TimeUnit.MILLISECONDS.sleep(3);
            }catch (Exception e){}
        }
    }
    public static void main(String [] args){
        new TriggerGC5().test();
    }
}
